package com.kangfawei.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author 康发伟
 * 通过继承Thread类来创建线程
 */
public class DownloadURL extends Thread {

    private String url;
    private String filename;
    private final File FILE_DIR = new File("E:/IO_target");

    public DownloadURL(String url, String filename){
        this.url = url;
        this.filename = filename;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 5; i++) {
                FileUtils.copyURLToFile(new URL(this.url), new File(FILE_DIR, filename+i+".html"));
                System.out.println(filename+"--"+i);
                Thread.sleep(300);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        DownloadURL pic1 = new DownloadURL("https://www.baidu.com","baidu");
        DownloadURL pic2 = new DownloadURL("https://www.zhihu.com","zhihu");
        pic1.start();
        pic2.start();
    }
}
